package com.gjintel.workflow.trade.service;

import com.gjintel.auction.business.model.AuctionInfo;
import com.gjintel.auction.business.service.IAuctionService;
import com.gjintel.business.webcontent.model.WebContent;
import com.gjintel.business.webcontent.service.IWebContentService;
import com.gjintel.framework.constant.ActivitiMap;
import com.gjintel.framework.dataset.service.impl.DataSetServiceImpl;
import com.gjintel.framework.holidayset.service.IHolidaySetService;
import com.gjintel.framework.res.model.Resources;
import com.gjintel.framework.res.service.IResService;
import com.gjintel.sdk.util.DateUtils;
import com.gjintel.sdk.util.WordHandler;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.Execution;
import org.apache.commons.io.FileUtils;
import org.aspectj.apache.bcel.generic.SWITCH;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.gjintel.framework.constant.Constant.PROJECTCATAGORY_TRANSFER;

/**
 * Created by Administrator on 2017/11/15.
 */
@Service("publishDocService")
public class PublishDocService {

    @Autowired
    private IResService resService;

    @Autowired
    private IAuctionService auctionService;

    @Autowired
    private IWebContentService webContentService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private IHolidaySetService holidaySetService;

    @Autowired
    private DataSetServiceImpl dataSetService;

    /**
     * 生成竞拍公示
     *
     * @param pi
     * @return
     */
    @SuppressWarnings("all")
    private WebContent generateAuctionPublicity(AuctionInfo pro) {
        WordHandler world = new WordHandler();
        Map map = new HashMap();
        // 合同附件
        String resName = "";
        List<Resources> resList = resService.findByBusinessIdAndFlag(pro.getId(), 1);
        for (Resources resource : resList) {
            resName += "<a href='../web/fileView?resId=" + resource.getId() + "'>" + resource.getOldName()
                    + "</a><br/> ";
        }
        //处置类
        if (pro.getCatagoryId().equals(PROJECTCATAGORY_TRANSFER)) {
            resList = resService.findByBusinessIdAndFlag(pro.getId(), 9);
            for (Resources resource : resList) {
                resName += "<a href='../web/fileView?resId=" + resource.getId() + "'>" + resource.getOldName()
                        + "</a><br/> ";
            }
        } else {
            resList = resService.findByBusinessIdAndFlag(pro.getId(), 10);
            for (Resources resource : resList) {
                resName += "<a href='../web/fileView?resId=" + resource.getId() + "'>" + resource.getOldName()
                        + "</a><br/> ";
            }
        }
        map.put("resName", resName);

        //生成内容
        world.generateWord("auctionPublicity.html", map, "auctionPublicity.html");// 生成内容worldXmlPath
        File file = new File("auctionPublicity.html");
        String content = "";
        try {
            content = FileUtils.readFileToString(file, "utf-8");
            file.delete();// 删除生成的文件
        } catch (IOException e) {
            e.printStackTrace();
        }

        WebContent web = new WebContent();
        // 标题
        String title = "【" + pro.getCatagoryName() + "】 " + pro.getAuctionName() + "（" + pro.getCode() + "）竞拍公告";

        web.setTitle(title);
        web.setContent(content);
        web.setCreaterId(ActivitiMap.SUPERADMIN_ID);

        web.setCreateTime(pro.getAuctionShowStart());
        web.setCreaterName("系统");
        web.setDeptId("1");
        web.setProjectId(pro.getId());
        web.setType("31");
        web.setIsDisplay(0);//显示

        web.setEndShowTime(pro.getAuctionShowEnd());// 设置展示结束时间
        return web;
    }


    /**
     * @param execution
     * @param docType   :AUCTION :发布竞拍公告，HIT:发布：发布中标公告，DOC:生成中标通知书
     */
    public void publish(Execution execution, String docType) {
        //文书类型

//        AUCTION :发布竞拍公告，HIT:发布：发布中标公告，DOC:生成中标通知书
//TODO　

        switch (docType) {
            //生成竞拍公告
            case "AUCTION":
                String processId = execution.getProcessInstanceId();
                AuctionInfo auctionInfo = auctionService.findByProcessId(processId);
                Date date = new Date();//竞价公示开始时间
//                int publicity = dataSetService.getDataSet().getAuctionPublicity();//竞价公告公示期
//                Date showEnd = DateUtils.computeWorkDayDate(holidaySetService, date, publicity);//公示结束时间
                Date showEnd = auctionInfo.getAuctionEnd();//公示结束时间
                auctionInfo.setAuctionShowStart(date);
                auctionInfo.setAuctionShowEnd(showEnd);
                WebContent webContent = generateAuctionPublicity(auctionInfo);
                webContentService.saveOrUpdate(webContent);
                runtimeService.setVariable(execution.getId(), "auctionPublicityDocumentId", webContent.getId());//竞价公告变量
                auctionService.saveOrUpdate(auctionInfo);
                break;
            case "HIT":

                break;
            case "DOC":

                break;
        }
        System.out.println("publishDocService.publish: " + docType);
    }
}
